<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

if ($EXCMS != "work") {
	header ("Location: ../../index.php");
	exit;
}

$numberusers = 30;
if (isset($_GET['id'])) $id = intval ($_GET['id']);
if (isset($_GET['page']) && $_GET['page'] != "0") $start = (intval ($_GET['page']) - 1) * $numberusers;
else $start = 0;
if (isset($_GET['order'])) $order_by = addslashes ($_GET['order']);
else $order_by = "user_id";

if (isset($_GET['id'])) {
	$get_user = $db->query ("SELECT * FROM ".DB_PEREFIX."_users WHERE user_id = '$id'");
	if ($db->num_rows($get_user) == 1) {
		$user = $db->fetch_array($get_user);
		
		past_navblock (array (
							array ("Профиль", "/users/$id.html", "name", "users"),
							array ("Публикации", "/articles/?user=$id", "user", $id),
			
					)
					);
		
		$file = file ("modules/auth/admin/fields_options.txt");
		$field = array ();
		
		foreach ($file as $f) {
			$fld = explode ("|", $f);
			//print "{$fld[0]}|{$fld[1]}|{$fld[2]}|{$fld[3]}|{$fld[4]}|{$fld[5]}<br>";
			(isset($fld[2])) ? $field[$fld[0]] = $fld : $field[$fld[0]] = trim($fld[1]);
		}
		// =============================
		// 0 - name
		// 1 - status
		// 2 - type
		// 3 - value (not for all)
		// 4 - label
		// 5 - important
		
		
		$PAGE_TITLE = $user['user_name'];
		
		$avatar = "/images/avatars/noavatar.gif";
		$http = "";
		$icq = "";
		$friends = "";
		
		if ($field['avatar'] == "on" && $user['user_avatar'] != "") $avatar = "/images/avatars/".$user['user_avatar'];
		if ($field['icq'] == "on" && $user['user_icq'] != "" && $user['user_icq'] != 0) $icq = "<img src=\"http://web.icq.com/whitepages/online?icq={$user['user_icq']}&img=5\" width=\"18\" height=\"18\" align=\"absmiddle\" /> <a href=\"http://wwp.icq.com/scripts/contact.dll?msgto={$user['user_icq']}\" target=\"_blank\">{$user['user_icq']}</a>";
		($user['user_ban'] == "off") ? $name = $user['user_name'] : $name = "<font color=\"red\">".$user['user_name']." (пользователю запрещен доступ на сайт)</font>";
		if ($field['http'] == "on" && $user['user_http'] != "") $http = "<a href=\"http://{$user['user_http']}\" target=\"_blank\">{$user['user_http']}</a>";
		
		$option = "";
		
		// ============================================== FRIENDS
		if ($field['friends'] == "on") {
			if (is_registered()) {
				if ($user['user_id'] != $GLOBAL_USER['user_id'] && !isset_friend($user['user_id'])) $option = "<a href=\"/\" onclick=\"add_friend ({$user['user_id']}); return false;\" title=\"Добавить в друзья\"><img src=\"/themes/$theme/images/user_add.gif\" alt=\"\" align=\"absmiddle\" /></a>";
				elseif (isset_friend($user['user_id'])) $option = "<a href=\"/\" onclick=\"add_friend ({$user['user_id']}); return false;\" title=\"Удалить пользователя из друзей\"><img src=\"/themes/$theme/images/user_delete.gif\" alt=\"\" align=\"absmiddle\" /></a>".
					" <a href=\"/messages/write/?to={$user['user_id']}\" title=\"Написать персональное сообщение\"><img src=\"/themes/$theme/images/user_message.gif\" alt=\"\" align=\"absmiddle\" /></a>";
			}
			
			$friends = print_friends ($user['user_id']);
			if ($friends == "") $friends = "<li class=\"none\">нет</li>";
		}
		// ============================================== KARMA
		if ($field['karma'] == "on") {
		
			$user_karma = $user['user_karma'];
			$votes = $user['karma_votes'];
			
		/*	if ($user['user_group'] == "admin") {
				$number = get_element ("{good_carma_cant}", "{/good_carma_cant}");
				$number = str_replace ("{KARMA}", "&infin;", $number);
				$number = str_replace ("{VOTES}", $votes, $number);
				$number = str_replace ("{USER_ID}", $user['user_id'], $number);
				$template->set_var("{KARMA}", $number);
				$karma = $template->display("user_karma.tpl");
			}
			else*/if ($user_karma[0] >= 0) {
				if (is_registered() && $user['user_id'] != $GLOBAL_USER['user_id'] && not_voted ($user['user_id']) && my_karma() >= 0) {
					$number = get_element ("{good_carma}", "{/good_carma}");
					$number = str_replace ("{USER_ID}", $user['user_id'], $number);
					$number = str_replace ("{VOTES}", $votes, $number);
					$number = str_replace ("{FUNCTION}", "change_karma", $number);
					$number = str_replace ("{KARMA}", number_format($user_karma, 2, ',', ''), $number);
					$template->set_var("{KARMA}", $number);
					$karma = $template->display("user_karma.tpl");
				}
				else {
					$number = get_element ("{good_carma_cant}", "{/good_carma_cant}");
					$number = str_replace ("{VOTES}", $votes, $number);
					$number = str_replace ("{KARMA}", number_format($user_karma, 2, ',', ''), $number);
					$number = str_replace ("{USER_ID}", $user['user_id'], $number);
					$template->set_var("{KARMA}", $number);
					$karma = $template->display("user_karma.tpl");
				}
			}
			elseif ($user_karma[0] < 0) {
				if (is_registered() && $user['user_id'] != $GLOBAL_USER['user_id'] && not_voted ($user['user_id']) && my_karma() >= 0) {
					$number = get_element ("{bad_carma}", "{/bad_carma}");
					$number = str_replace ("{VOTES}", $votes, $number);
					$number = str_replace ("{USER_ID}", $user['user_id'], $number);
					$number = str_replace ("{FUNCTION}", "change_karma", $number);
					$number = str_replace ("{KARMA}", number_format($user_karma, 2, ',', ''), $number);
					$number = str_replace ("{USER_ID}", $user['user_id'], $number);
					$template->set_var("{KARMA}", $number);
					$karma = $template->display("user_karma.tpl");
				}
				else {
					$number = get_element ("{bad_carma_cant}", "{/bad_carma_cant}");
					$number = str_replace ("{VOTES}", $votes, $number);
					$number = str_replace ("{KARMA}", number_format($user_karma, 2, ',', ''), $number);
					$number = str_replace ("{USER_ID}", $user['user_id'], $number);
					$template->set_var("{KARMA}", $number);
					$karma = $template->display("user_karma.tpl");
				}
			}
		}
		
		
		// ============================================== BLOGS
		
		if ($user['user_blogs'] == "") $blogs = "";
		else {
			$blogs = "<ul id=\"communities_list\">\n";
			$all = explode (",", $user['user_blogs']);
			foreach ($all as $blog) {
				if ($blog != "") {
					$get_bl = $db->fetch_array ($db->query ("SELECT cat_name, cat_header FROM ".DB_PEREFIX."_art_category WHERE cat_id = '$blog'"));
					if ($get_bl['cat_name'] != "") $blogs .= "<li class=\"community\"><a href=\"/articles/{$get_bl['cat_name']}/\">{$get_bl['cat_header']}</a></li>\n";
					else $blogs .= "<li class=\"community\"><acronym title=\"блог был удален\">удалено</acronym></li>";
				}
			}
			$blogs .= "</ul>";
		}
				
		
		// ============================================== COMMENTS
		
		if ($filed['comments'] == "on") {
			if ($_GET['del_com'] == "true" && isset ($_GET['com_id'])) delete_comment ($_GET['com_id']);
			if (isset($_POST['com_body'])) {
				add_comment ($_POST['com_name'], $_POST['com_mail'], $_POST['com_body'], 'users', $id, $_POST['code']);
			}
			if (isset($_GET['delete_com']) && $GLOBAL_USER['user_id'] == $id) {
				$comment_id = intval($_POST['delete_com']);
				$del = $db->query ("DELETE FROM ".DB_PEREFIX."_comments WHERE com_id = '$comment_id'");
				if ($del) {
					$get = $db->query ("SELECT com_id, com_parent FROM ".DB_PEREFIX."_comments WHERE com_category = 'users' AND com_idi = '$id'");
					$array = array ();
					while (list($the_id, $parent) = $db->fetch_row ($get)) $array[$parent][] = $the_id;
					delete_com ($array[$comment_id], $array);
					site_message ("Цепочка комментариев удалена!");
				}
				else site_error ("Произошла некая ошибка!");
			}
		}
		
		$form .= "\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">";
		if ($field['avatar'] == "on") $form .= "\n<tr><td width=\"20%\" align=\"right\">Аватар:</td><td><img src=\"$avatar\" title=\"".$user['user_name']."\" /></td></tr>";
		if ($field['icq'] == "on" && $icq != "") $form .= "\n<tr><td width=\"20%\" align=\"right\">ICQ:</td><td>$icq</td></tr>";
		if ($field['http'] == "on" && $http != "") $form .= "\n<tr><td width=\"20%\" align=\"right\">Сайт:</td><td><b>$http</b></td></tr>";
		
		$form .= "\n<tr><td align=\"right\">Присоединился:</td><td><b>".format_time($user['user_date'])."</b></td></tr>";
		
		// =============================
		// 0 - name
		// 1 - status
		// 2 - type
		// 3 - value (not for all)
		// 4 - label
		// 5 - important
		for ($i=1; $i<=9; $i++) {
			if (trim($field['ad'.$i][1]) == "on" && $field['ad'.$i][2] != "checkbox") {
				if ($user['additional'.$i] != "" || $user['additional'.$i] != 0) $form .= "\n<tr><td valign=\"top\" align=\"right\">".$field['ad'.$i][4]."</td><td valign=\"top\"><b>".n2br($user['additional'.$i])."</b></td></tr>";
			}
		}
		
		if ($field['friends'] == "on") $form .= "\n<tr><td valign=\"top\" align=\"right\">Друзья:</td><td><ul class=\"friends_list\">$friends</ul></td></tr>";
		if ($blogs != "") $form .= "\n<tr><td valign=\"top\" align=\"right\">Состоит в:</td><td>$blogs</td></tr>";
		
		$form .= "</table>";
		
		past_table ("Профиль ".$user['user_name']." ".user_status($user['user_id'])." <div style=\"display:inline;\" id=\"friends_list_false\">".$option."</div>", $form);
		
		
		//if ($field['karma'] == "on") print $karma;
		
	
				
		if ($user['allow_comments'] == "on" && $filed['comments'] == "on") {
			print "\n<a name=\"comments\"></a><h5>Стена</h5>\n<div id=\"comments_div\">\n";
			print_comments ('users', $id, "/users/$id.html");
			print "</div>";
			past_com_table ("/users/$id.html");
		}
	}
	else site_error ("Нет такого пользователя или, возможно, он сменил имя");
}
else error_404();

function print_friends ($id) {
	global $GLOBAL_USER, $db;
	$my_friends = $db->fetch_array ($db->query ("SELECT user_friends FROM ".DB_PEREFIX."_users WHERE user_id = '$id'"));
	$friends = $my_friends['user_friends'];
	$friends = explode (",", $friends);
	$string = "";
	//for ($i=1; $i<count($friends); $i++) {
	foreach ($friends as $friend) {
		if ($friend != "") {
			$get_name = $db->fetch_array ($db->query ("SELECT user_name FROM ".DB_PEREFIX."_users WHERE user_id = '$friend'"));
			$string .= "<li class=\"friend\"><a href=\"/users/$friend.html\">{$get_name['user_name']}</a></li>\n";
		}
	}
	return $string;
}

function isset_friend ($id) {
	global $GLOBAL_USER, $db;
	$my_friends = $db->fetch_array ($db->query ("SELECT user_friends FROM ".DB_PEREFIX."_users WHERE user_id = '{$GLOBAL_USER['user_id']}'"));
	$friends = $my_friends['user_friends'];
	$friends = explode (",", $friends);
	foreach ($friends as $friend) if ($friend == $id) return true;
	return false;
}

function delete_com ($arr, $array) {
	global $db;
	foreach ($arr as $comment) {
		$db->query ("DELETE FROM ".DB_PEREFIX."_comments WHERE com_id = '$comment'");
		if (isset($array[$comment])) delete_comment ($array[$comment], $array);
	}
}

function not_voted ($user_id) {
	global $GLOBAL_USER;
	
	$votes = explode (",", $GLOBAL_USER['my_votes']);
	foreach ($votes as $v) if (trim($v) == $user_id) return false;
	return true;
}
